<?php

namespace app\business\controller;
use think\Controller;
use Util\SysDb;
use think\Db;
class GetExpressAmount extends Controller
{
    public function GetExpressAmount( $businfo,$expresssheetid,$weight,$tenantid,$tenantviplevel){
        $outAmount="";
        $expressSheetId=$expresssheetid;
        $tenantId=$tenantid;
        $Weight=$weight;
        $tenantVipLevel=$tenantviplevel;
        $this->db = new SysDb();
        $sysExpressFee = $this->db->table('sys_express_fee')->where(['express_sheet_id' => $expressSheetId])->item();
        if( floatval($sysExpressFee['max_weight'])<$weight){
            $outWeight=$Weight-floatval($sysExpressFee['max_weight']);
            $outAmount=$outWeight*1;
        }
        $tenantExpressFeeList= Db::table('tenant_express_fee')
            ->alias(' tef')
            ->join('sys_express_fee sef', 'tef.express_fee_id = sef.id',"left")
            ->join('tenant_vip_level tvl', 'tvl.tenant_id = tef.tenant_id',"left")
            ->where(['express_sheet_id' => $expressSheetId])
            ->where(['tef.tenant_id'=>$tenantId])
            ->where(['name'=>$tenantVipLevel])
            ->field(array(' tef.id','tef.profit','tef.cost'=>"tenantCost",'sef.cost','sef.max_weight','sef.express_sheet_id'=>'expressSheetId',"tvl.discount","tvl.tenant_id"))
            ->select();

        if (!$tenantExpressFeeList) {
            exit(json_encode(['code' => 1, 'message' => 'error', "result" => "快递费用获取失败"]));
        } 
        $expressAmount=$tenantExpressFeeList['profit']+$tenantExpressFeeList['cost'];
       if(null !== $outAmount){
        $expressAmount=$expressAmount+$outAmount;
       }
       $expressAmount=$expressAmount*$tenantExpressFeeList['discount'];
       $result['expressAmount']=$expressAmount;
       $result['tenantProfit']=$tenantExpressFeeList['profit'];
       $result['tenantCost']=$tenantExpressFeeList['tenantCost'];
       $result['cost']=$tenantExpressFeeList['cost'];
       exit(json_encode(['code' => 1, 'message' => 'error', "result" =>$result]));

}
}
